package example.demo.controller;

import demo.rao.mideng.annotation.RaoMiDeng;
import example.demo.entity.User;
import example.demo.service.MiDengService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * update += 才会有幂等性问题
 * 超时重试导致的重复下单 insert
 */
@RestController
@RequestMapping("/rao/mi-deng/MiDengController")
public class MiDengController {

    @Autowired
    MiDengService miDengService;

    /*
     * http://localhost:8080/rao/mi-deng/MiDengController/update
     * {
     *     "id":"1",
     *     "age":123,
     *     "name":"大千世界"
     * }
     */
    @RequestMapping("/update")
    public String update(@RequestBody User user) {
        int i = miDengService.update(user);
        return i == 0 ? "修改-fail" : "修改-succeed";
    }

    //测试update +- 操作的
    // http://localhost:8080/rao/mi-deng/MiDengController/updateIncrAge/1
    @RequestMapping("/updateIncrAge/{userId}")
    public String updateIncrAge(@PathVariable Integer userId) {
        return miDengService.updateIncrAge(userId) == 1 ? "更新--succeed" : "更新-fail";
    }

    /*
     * http://localhost:8080/rao/mi-deng/MiDengController/insert
     * {
     *     "age":123,
     *     "name":"大千世界"
     * }
     */
    @RequestMapping("/insert")
    public Object insert(@RequestBody User user) {
        int i = miDengService.insert(user);
        return i == 0 ? "insert-fail" : "insert-succeed";
    }

    @RequestMapping("/query")
    public User query(@RequestParam String name, @RequestParam String realName) {
        User paramObj = new User(null, name, null, realName);
        User user = miDengService.query(paramObj);
        return user;
    }
}
